package org.example.erat.controller;

import org.example.erat.model.ReportAnalyzer;
import org.example.erat.model.Student;
import org.example.erat.model.StudentManager;
import org.example.erat.model.export.ExportStrategy;
import org.example.erat.model.export.ExportStrategyFactory;
import org.example.erat.model.importing.ImportStrategy;
import org.example.erat.model.importing.ImportStrategyFactory;
import org.example.erat.view.View;

import java.io.File;
import java.util.List;
import java.util.Map;

public class EratController {
    private final View view;
    private final StudentManager studentManager;
    private final ReportAnalyzer reportAnalyzer;
    private final ImportStrategyFactory importStrategyFactory;
    private final ExportStrategyFactory exportStrategyFactory;

    public EratController(View view) {
        this.view = view;
        this.studentManager = new StudentManager();
        this.reportAnalyzer = new ReportAnalyzer(studentManager);
        this.importStrategyFactory = new ImportStrategyFactory();
        this.exportStrategyFactory = new ExportStrategyFactory();
    }

    public void importStudents(File file, String format) {
        try {
            ImportStrategy strategy = importStrategyFactory.createStrategy(format);
            List<Student> students = strategy.importStudents(file);

            if (students.isEmpty()) {
                view.showMessage("警告", "导入的学生列表为空");
                return;
            }

            studentManager.addStudents(students);
            view.displayStudents(students);
            view.showMessage("成功", "成功导入 " + students.size() + " 名学生信息");
        } catch (Exception e) {
            view.showError("导入失败: " + e.getMessage());
        }
    }

    public void analyzeReports(String reportsDirectoryPath) {
        try {
            if (studentManager.getAllStudents().isEmpty()) {
                view.showMessage("提示", "请先导入学生信息");
                return;
            }

            reportAnalyzer.analyzeReports(reportsDirectoryPath);

            // 获取分析结果
            Map<String, List<String>> missingReports = reportAnalyzer.getStudentStatistics();
            Map<String, List<String>> experimentStatistics = reportAnalyzer.getExperimentStatistics();

            // 更新视图
            view.displayMissingReports(missingReports);
            view.displayExperimentStatistics(experimentStatistics);
            view.setReportAnalyzer(reportAnalyzer);

            view.showMessage("成功", "实验报告分析完成");
        } catch (Exception e) {
            view.showError("分析失败: " + e.getMessage());
        }
    }

    public void exportStatistics(File directory, String format) {
        try {
            if (studentManager.getAllStudents().isEmpty()) {
                view.showMessage("提示", "请先导入学生信息并分析实验报告");
                return;
            }

            ExportStrategy strategy = exportStrategyFactory.createStrategy(format);

            // 获取统计数据
            Map<String, List<String>> studentStatistics = reportAnalyzer.getStudentStatistics();
            Map<String, List<String>> experimentStatistics = reportAnalyzer.getExperimentStatistics();

            // 导出数据
            strategy.exportStatistics(directory, studentStatistics, experimentStatistics);

            view.showMessage("成功", "统计数据已成功导出到: " + directory.getAbsolutePath());
        } catch (Exception e) {
            view.showError("导出失败: " + e.getMessage());
        }
    }
}